import { getDestination, PdfJs, SpecialZoomLevel } from "../../core";

import BookmarkItem from "./BookmarkItem";
import { FC } from "react";

interface BookmarkListProps {
  bookmarks: PdfJs.Outline[];
  depth: number;
  doc: PdfJs.PdfDocument;
  onJumpToDest(
    pageIndex: number,
    bottomOffset: number,
    scaleTo: number | SpecialZoomLevel
  ): void;
}

const BookmarkList: FC<BookmarkListProps> = ({
  bookmarks,
  depth = 0,
  doc,
  onJumpToDest,
}) => {
  const jumpToDest = (dest: PdfJs.OutlineDestinationType): void => {
    getDestination(doc, dest).then((target) => {
      const { pageIndex, bottomOffset, scaleTo } = target;
      onJumpToDest(pageIndex + 1, bottomOffset, scaleTo);
    });
  };

  return (
    <ul className="rpv-bookmark__list">
      {bookmarks.map((bookmark, index) => {
        return (
          <li key={index}>
            <BookmarkItem
              bookmark={bookmark}
              depth={depth}
              doc={doc}
              onClick={jumpToDest}
              onJumpToDest={onJumpToDest}
            />
          </li>
        );
      })}
    </ul>
  );
};

export default BookmarkList;
